package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.dao.Family_memberDAO;
import com.gxuwz.medical.domain.family_member.Family_member;

public class Family_memberServlet extends BaseServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m = request.getParameter("m");// 动作类型参数
		if ("list".equals(m)) {
			process(request, response, "/page/family_member/familyMember_list.jsp");
		} else if ("input".equals(m)) {
			process(request, response, "/page/family_member/familyMember_add.jsp");
		} else if ("get".equals(m)) {
			process(request, response, "/page/family_member/familyMember_edit.jsp");
		} else if ("add".equals(m)) {
			add(request, response);
			process(request, response, "/page/family_member/familyMember_list.jsp");
		} else if ("edit".equals(m)) {
			this.edit(request, response);
		} else if ("del".equals(m)) {
			this.del(request, response);
		} else if ("getDetails".equals(m)) {
			process(request, response, "/page/family_member/familyMember_detailsList.jsp");
		} else if ("find".equals(m)) {
			this.find(request, response);
		}
	}

	private void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1：接收参数
		String familycode = req.getParameter("id");
		// 2:构造新慢病信息对象
		Family_memberDAO model = new Family_memberDAO();
		try {
			List<Family_member> word = model.findFamilyList(familycode);
			write(word, req, resp);
			// process(req, resp, "/page/join_nh/join_nh_list.jsp");
		} catch (Exception e) {
			error(req, resp);
		}

	}

	private void add(HttpServletRequest rs, HttpServletResponse response) throws ServletException, IOException {
		// 接收页面传递过来的参数
		String join_nh_code = rs.getParameter("join_nh_code");// 农合证号
		String familycode = rs.getParameter("familycode");// 家庭编号
		String membername = rs.getParameter("membername");// 姓名
		String IDcard = rs.getParameter("IDcard");// 身份证号码
		String state = rs.getParameter("state");// 人员属性
		String relation = rs.getParameter("relation");// 与 户 主 关系
		String userid = rs.getParameter("userid");
		String familyteam = rs.getParameter("familyteam");// 户 内编号
		String age = rs.getParameter("age");
		String birthday = rs.getParameter("birthday");
		String tel = rs.getParameter("tel");
		String health = rs.getParameter("health");
		String nation = rs.getParameter("nation");
		String sex = rs.getParameter("sex");
		String IScountryside = rs.getParameter("IScountryside");
		String occupation = rs.getParameter("occupation");// 职业
		String address = rs.getParameter("address");
		String medicalCard = rs.getParameter("medicalCard");

		// 实例化Family类
		Family_member family_member = new Family_member();
		// 调用添加方法
		try {

			family_member.addFamily_member(join_nh_code, familycode, membername, IDcard, state, relation, userid, age,
					birthday, tel, health, familyteam, nation, sex, IScountryside, occupation, address, medicalCard);
		} catch (Exception e) {
			error(rs, response);
		}

	}

	private void edit(HttpServletRequest rs, HttpServletResponse resp) throws ServletException, IOException {

		// 1：接收参数
		String join_nh_code = rs.getParameter("join_nh_code");// 农合证号
		String familycode = rs.getParameter("familycode");// 家庭编号
		String membername = rs.getParameter("membername");// 姓名
		String IDcard = rs.getParameter("IDcard");// 身份证号码
		String state = rs.getParameter("state");// 人员属性
		String relation = rs.getParameter("relation");// 与 户 主 关系
		String userid = rs.getParameter("userid");
		String familyteam = rs.getParameter("familyteam");// 户 内编号
		String age = rs.getParameter("age");
		String birthday = rs.getParameter("birthday");
		String tel = rs.getParameter("tel");
		String health = rs.getParameter("health");
		String nation = rs.getParameter("nation");
		String sex = rs.getParameter("sex");
		String IScountryside = rs.getParameter("IScountryside");
		String occupation = rs.getParameter("occupation");// 职业
		String address = rs.getParameter("address");
		String medicalCard = rs.getParameter("medicalCard");

		try {

			Family_member model = new Family_member(join_nh_code, familycode, membername, IDcard, state, relation,
					userid, familyteam, age, birthday, tel, health, nation, sex, IScountryside, occupation, address,
					medicalCard);

			// 3：调用保存的方法
			model.edit();
			process(rs, resp, "/page/family_member/familyMember_list.jsp");
		} catch (Exception e) {
			error(rs, resp);
		}

	}

	private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 1：接收参数
		String familycode = req.getParameter("id");

		// 2:构造新慢病信息对象
		Family_member model = new Family_member();
		try {
			model.del(familycode);
			process(req, resp, "/page/family_member/familyMember_list.jsp");
		} catch (Exception e) {
			error(req, resp);
		}

	}

}
